import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.util.Vector;
import java.applet.*;

import javax.swing.*;

import netscape.javascript.JSException;
import netscape.javascript.JSObject;

public class DoodlePassword extends JApplet{
	protected  DoodleCanvas canvas;
	protected EventListener listener;
	protected boolean isApplet = false;
	
//	private JTextField textPass;
//	private JPanel panelTextPass;
	
	protected static int frameW= 400;
	protected static int frameH= 400;
	
	protected PasswordGen password;
	protected JSObject win;
	
	
	protected ControlPanel panel;
	
	//parameters to be retrieved
	protected String mode="Valor Inicial";
	protected int n_col=3,n_row=3;
	protected String system= "";
	protected String username = "";
	protected RunScript rs = new RunScript();
	
	public DoodlePassword(){
		
		//this.isApplet = isApplet;
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(makeCanvas(), BorderLayout.CENTER);
		
		makePassword();
		listener = makeCanvasListener();
		canvas.addMouseListener((MouseListener) listener);
		canvas.addMouseMotionListener((MouseMotionListener) listener);
		
	}
//	public DoodlePassword(){
//		//invoked as an applet
//		this(true);
//	}
	
	protected Component makeCanvas(){
		canvas = new DoodleCanvas();
		return canvas;
	}
	
	
	protected void makePassword(){
		password = new PasswordGen(canvas, frameW, frameH);
	}
	
	
	protected EventListener makeCanvasListener() {
		return new DoodleCanvasListener(canvas, password);
	}
		
//	public static void main(String[] args){
//		JFrame frame = new JFrame();
//		frame.setTitle("Scribble Pad");
//		frame.getContentPane().setLayout(new BorderLayout());
//		frame.getContentPane().add(new Doodle (false), BorderLayout.CENTER);
//		frame.addWindowListener(new AppCloser());
//		frame.pack();
//		frame.setSize(frameW, frameH);
//		frame.show();
//	}
	
	static class AppCloser extends WindowAdapter{
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	}
	
	
	public void init(){
		try{
			//getParams();
			win = JSObject.getWindow(this);
			//win.call("testeJS", null);
			
			
		} 
		catch(JSException jse) {
				jse.printStackTrace();
				} catch (Exception e) {
				e.printStackTrace();
		}
		
		//DoodlePassword dp = new DoodlePassword ();
		
		
		 
		panel = new ControlPanel(canvas, password,win);
		
		
		
		this.getContentPane().add(panel, BorderLayout.SOUTH);
		getParams();
		
		
		//this.addWindowListener(new AppCloser());
		//this.pack();
		//this.setResizable(false);
		//this.setSize(frameW, frameH);
		
		this.show();

		
		
		
		
	}
	
	 //get parameters from parent page
	   private void getParams(){
	       system = getParameter("system");
	       username = getParameter("user");
	       mode = getParameter("mode");
	       n_row = Integer.parseInt(getParameter("nRow"));
	       n_col = Integer.parseInt(getParameter("nCol"));
	             
	       
	       setParameters();
	   }
	
	public void setParameters(){
		panel.setMode(mode);
		canvas.setN_Columns(n_col);
		canvas.setN_Rows(n_row);
		
		password.setMode(mode);
		
		rs.username=this.username;
		rs.system=this.system;
		password.setRunScript(rs);
		panel.setRunScript(rs);

	}
	

	
}
